package ${project.packageName}.${project.dataCenterType}.ctrl;

import java.util.HashMap;
import java.util.Map;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import ${project.packageName}.${project.dataCenterType}.dao.${restApi.tableName?cap_first}Dao;
import ${project.packageName}.${project.dataCenterType}.constant.BaseConstant;
import ${project.packageName}.${project.dataCenterType}.entity.JsonResult;
import ${project.packageName}.${project.dataCenterType}.entity.${restApi.tableName?cap_first};
import ${project.packageName}.${project.dataCenterType}.utils.DBUtils;
@RestController
@RequestMapping("/${project.version}")
public class ${ctrlName}Controller {
    
    @Autowired
    private ${restApi.tableName?cap_first}Dao ${restApi.tableName?lower_case}Dao;
    
    /**
     * 日志对象
     */
    private static final Logger LOG = LoggerFactory.getLogger(${ctrlName}Controller.class);
    <#if responseDataType == "object">
    @RequestMapping(value = "${restApi.requestUrl}", method = RequestMethod.${restApi.requestType})
    public Object ${methodName}(HttpServletRequest request<#list paraMaps as para>, @RequestParam(required=${para.isMust}) ${para.type} ${para.name}</#list>){
        Map<String,Object> map = new HashMap<String,Object>();
        ${restApi.tableName?cap_first} ${restApi.tableName?lower_case} = ${restApi.tableName?lower_case}Dao.findOne(<#list paraMaps as para>${para.name}</#list>);
        if(!StringUtils.isEmpty(${restApi.tableName?lower_case})){
            map.put("code", BaseConstant.CODE_SUCCESS);
            map.put("${restApi.tableName?lower_case}", ${restApi.tableName?lower_case});
            LOG.info(JSON.toJSONString(${restApi.tableName?lower_case}));
        }else{
            map.put("code", BaseConstant.CODE_ERROR);
        }
        JSONObject returnObj = new JSONObject();
        returnObj.put("Info", ${restApi.tableName?lower_case});
        return JSON.toJSONString(returnObj);
    }
    </#if>
    
    <#if responseDataType == "list">
    @RequestMapping(value="${restApi.requestUrl}",method = RequestMethod.${restApi.requestType})
    public List<${restApi.tableName?cap_first}> ${methodName}(HttpServletRequest request<#list paraMaps as para>,@RequestParam(required=${para.isMust}) ${para.type} ${para.name}</#list>) throws Exception{
       <#list paraMaps as para>
       <#if para.dataType??>    
   	    <#if para.dataType=="2">
        ${para.name} -= 1;
       	</#if>
        Pageable pageable = new PageRequest(<#list paraMaps as para><#if para.dataType??><#if para.dataType=="2">${para.name},</#if><#if para.dataType=="1">${para.name},</#if></#if></#list>null);
    	<#if sql??>
		Page<${restApi.tableName?cap_first}> ${restApi.tableName?lower_case} = ${restApi.tableName?lower_case}Dao.findSome(<#list paraMaps as para><#if !para.dataType??>${para.name}<#if para_has_next>,</#if></#if></#list>,pageable);
    	<#else>
    	Page<${restApi.tableName?cap_first}> ${restApi.tableName?lower_case} = ${restApi.tableName?lower_case}Dao.findAll(pageable);
    	</#if>
    	List<${restApi.tableName?cap_first}> list = ${restApi.tableName?lower_case}.getContent();
    	Map<String,Object> map = new HashMap<String,Object>();
    	if(list.size()==0){
    		map.put("code","404");
    	}else{
	    	map.put("data",list);
    		map.put("code","200");
    		map.put("count",${restApi.tableName?lower_case}.getTotalElements());
    	}
    	return list;
       <#break>
       <#else>
       <#if sql??>
	   String sql = "${sql}";
	   <#list paraMaps as para>
	   sql = sql.replace("{${para.name}}",<#if para.type=="String">"${para.name}"</#if><#if para.type=="long">Long.toString(${para.name})</#if><#if para.type=="int">String.valueOf(${para.name})</#if>);
	   </#list>
	   List list = DBUtils.query(sql);
	   return list;
       <#else>
       List<${restApi.tableName?cap_first}> ${restApi.tableName?lower_case} = ${restApi.tableName?lower_case}Dao.findAll();
       return ${restApi.tableName?lower_case};
       </#if>
       <#break>
       </#if>
       </#list>
   }
   </#if>
   
   <#if restApi.responseDataType == "count">
   @RequestMapping(value = "${restApi.requestUrl}", method = RequestMethod.${restApi.requestType})
   public Object ${methodName}(HttpServletRequest request<#list paraMaps as para>, @RequestParam(required=${para.isMust}) ${para.type} ${para.name}</#list>){
       Map<String,Object> map = new HashMap<String,Object>();
       List<${restApi.tableName?cap_first}> list = ${restApi.tableName?lower_case}Dao.findAll();
       if(list.size()==0){
           map.put("code", "404");
           map.put("message", "查询失败或结果集为空");
       }else{
           map.put("code", "200");
           map.put("message", "成功");
       }
       map.put("count",list.size());
       return JSON.toJSONString(map);
   }
   </#if>    

}